package com.sghd.candy.module.fight.facade;

import com.sghd.candy.module.bag.service.PlayerBagService;
import com.sghd.candy.module.box.service.BoxService;
import com.sghd.candy.module.box.service.PlayerBoxService;
import com.sghd.candy.module.element.service.PlayerElementService;
import com.sghd.candy.module.fight.service.FightService;
import com.sghd.candy.module.player.manager.Player;
import com.sghd.candy.module.player.service.PlayerService;
import com.sghd.pojo.candy.bag.enums.PropType;
import com.sghd.pojo.candy.fight.EnterVo;
import com.sghd.pojo.candy.fight.dto.SettleTo;
import com.sghd.pojo.candy.fight.vo.ServerVo;
import com.sghd.pojo.fight.game.LevelVo;
import com.sghd.pojo.fight.game.RoomType;
import com.sghd.pojo.fight.game.SettleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

import static com.sghd.pojo.StaticConfig.OTHER_DROP_MONEY;

/**
 * @author Allen
 * @date 2018/12/03
 */
@Component
public class FightFacadeImpl implements FightFacade {
    private static final Logger logger = LoggerFactory.getLogger(FightFacadeImpl.class);
    @Autowired
    FightService fightService;
    @Autowired
    BoxService boxService;
    @Autowired
    PlayerService playerService;
    @Autowired
    private PlayerBagService playerBagService;
    @Autowired
    private PlayerBoxService playerBoxService;
    @Autowired
    private PlayerElementService playerElementService;

    @Override
    public ServerVo getServer(long playerId) {
        return fightService.getFightServer(playerService.load(playerId));
    }

    @Override
    public void connect(String ip, EnterVo enter) {
        Player player = playerService.load(enter.getPlayerId());
        fightService.connect(player, enter.getType());
    }

    @Override
    public SettleVo settlement(String ip, SettleTo settleTo) {
        SettleVo settleVo = SettleVo.valueOf(settleTo.getRoomType(), settleTo.getSn(),
                settleTo.getWinId(), settleTo.getReason());

        if (settleTo.getRoomType() == RoomType.PVP) {
            // 平局判断
            if (settleTo.getLeftBlood() == 0) {
                fightService.pvpLoseSettlement(playerService.load(settleTo.getWinId()), settleTo.getWinGetMoney(), settleVo);
            } else {
                fightService.pvpWinSettlement(playerService.load(settleTo.getWinId()), settleTo.getInitBlood(), settleTo.getLeftBlood(),
                        settleTo.getWinGetMoney(), settleVo);
            }
            fightService.disConnect(playerService.load(settleTo.getWinId()));
            fightService.pvpLoseSettlement(playerService.load(settleTo.getLoseId()), settleTo.getLoseGetMoney(), settleVo);
            fightService.disConnect(playerService.load(settleTo.getLoseId()));
        } else {
            LevelVo levelVo = new LevelVo(settleTo.getBigLevelId(), settleTo.getSmallLevelId());
            settleVo.setLevel(levelVo);
            fightService.pveSettlement(playerService.load(settleTo.getWinId()), settleTo.getWinGetMoney(), settleTo.getLeftBlood(), settleVo);
            fightService.disConnect(playerService.load(settleTo.getWinId()));
        }
        // 结算入库
        settleVo.getDropVoMap().forEach((playerId, dropVo) -> {
            Player player = playerService.load(playerId);
            float otherDropMoneyRate = playerService.loadOther(player, OTHER_DROP_MONEY,0F, Float.class);
            long otherDropMoney = new BigDecimal(String.valueOf(otherDropMoneyRate)).multiply(new BigDecimal(dropVo.getDropMoney())).longValue();
            settleVo.getDropVoMap().get(playerId).setOtherDropMoney(otherDropMoney);
            // 游戏币入库
            playerBagService.change(player, PropType.MONEY, dropVo.getDropMoney() + dropVo.getOtherDropMoney());
            // 五行点入库
            playerElementService.change(player, dropVo.getDropElement());
            // 钥匙入库
            dropVo.getBoxKeys().forEach((keyId, count) -> playerBoxService.change(player, keyId, count));
        });

        return settleVo;
    }

    @Override
    public void disConnect(String ip, long playerId) {
        fightService.disConnect(playerService.load(playerId));
    }

    @Override
    public void cancelReConn(String ip, long playerId) {
        fightService.disConnect(playerService.load(playerId));
    }

    @Override
    public Integer levelSelect(long playerId, LevelVo levelVo) {
        return fightService.levelSelect(playerService.load(playerId), levelVo);
    }
}
